package com.example.practice.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.practice.R;
import com.example.practice.utils.SharedpreferencesUtils;

public class securitySettingActivity extends AppCompatActivity {
    private TextView tv_title;
    private TextView tv_back;
    private RelativeLayout rl_title_bar;
    private EditText et_validate_name_yl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_security_setting);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        initView();
    }

    private void initView() {
        tv_title=findViewById(R.id.tv_title);
        tv_title.setText("设置密保");
        tv_back=findViewById(R.id.tv_back);
        rl_title_bar=findViewById(R.id.rl_bar);
        et_validate_name_yl=findViewById(R.id.et_validate_name_yl);
        Button btn_save_yl=findViewById(R.id.btn_save_yl);

        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                securitySettingActivity.this.finish();
            }
        });

        btn_save_yl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String validateName=et_validate_name_yl.getText().toString().trim();
                if (TextUtils.isEmpty(validateName)){
                    Toast.makeText(securitySettingActivity.this,"请输入要验证的姓名",Toast.LENGTH_SHORT).show();
                    return;
                }
                else {
                    Toast.makeText(securitySettingActivity.this,"密保设置成功",Toast.LENGTH_SHORT).show();
                    saveSecurity(validateName);
                    securitySettingActivity.this.finish();
                }
            }
        });
    }

    /**
     * 将用户密保到SP中
     */
    private void saveSecurity(String validateName){
        SharedPreferences sp =getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString(SharedpreferencesUtils.readLoginUserName(this)+"_security", validateName);
        editor.commit();
    }
}